package server

import (
	"errors"
	"github.com/doujunyu/gogo/gogo_log"
	"github.com/doujunyu/gogo/sql_aid"
	"go_self/model"
	"go_self/util"
	"strings"
	"time"
)

// UserAddressByAdd 收货地址添加
func UserAddressByAdd(userId int64, input map[string]string) error {
	if input["name"] == "" || input["mobile"] == "" || input["address"] == "" {
		return errors.New("缺少参数")
	}
	if !util.IsMobile(input["mobile"]) {
		return errors.New("手机号不正确")
	}
	tx, err := model.PgSql.Begin()
	defer tx.Rollback()
	//默认选中
	if input["is_default"] == "1" {
		update := make(map[string]interface{})
		update["is_default"] = model.UserAddressByDefaultNo
		updateSql, args := sql_aid.PgTable(model.TableByUserAddress).Where("user_id = ?", userId).UpdateByMap(&update)
		_, err := tx.Exec(updateSql, args...)
		if err != nil {
			gogo_log.Error("server_UserAddressByAdd", "重置用户默认选项失败", err.Error())
			return errors.New("添加失败")
		}
	} else {
		input["is_default"] = model.UserAddressByDefaultNo
	}
	//时间
	thisTime := time.Now().Format(time.RFC3339)
	insertData := make(map[string]interface{})
	insertData["user_id"] = userId
	insertData["name"] = input["name"]
	insertData["mobile"] = input["mobile"]
	insertData["address"] = input["address"]
	insertData["address_code"] = input["address_code"]
	insertData["address_info"] = input["address_info"]
	insertData["is_default"] = input["is_default"]
	insertData["time_c"] = thisTime
	insertData["time_u"] = thisTime

	userAddressSql, args := sql_aid.PgTable(model.TableByUserAddress).InsertByMap(&insertData)
	_, err = tx.Exec(userAddressSql, args...)
	if err != nil {
		gogo_log.Error("server_UserAddressByAdd", "收货地址添加", err.Error())
		return errors.New("添加失败")
	}
	tx.Commit()
	return nil
}

// UserAddressByEdit 收货地址编辑
func UserAddressByEdit(userId int64, input map[string]string) error {
	if input["id"] == "" || input["name"] == "" || input["mobile"] == "" || input["address"] == "" {
		return errors.New("缺少参数")
	}
	if !util.IsMobile(input["mobile"]) {
		return errors.New("手机号不正确")
	}
	tx, err := model.PgSql.Begin()
	defer tx.Rollback()
	//默认选中
	if input["is_default"] == "1" {
		updateDefault := make(map[string]interface{})
		updateDefault["is_default"] = model.UserAddressByDefaultNo
		updateSql, args := sql_aid.PgTable(model.TableByUserAddress).Where("user_id = ?", userId).UpdateByMap(&updateDefault)
		_, err := tx.Exec(updateSql, args...)
		if err != nil {
			gogo_log.Error("server_UserAddressByEdit", "重置用户默认选项失败", err.Error())
			return errors.New("添加失败")
		}
	} else {
		input["is_default"] = model.UserAddressByDefaultNo
	}
	//时间
	thisTime := time.Now().Format(time.RFC3339)
	update := make(map[string]interface{})
	update["name"] = input["name"]
	update["mobile"] = input["mobile"]
	update["address"] = input["address"]
	update["address_code"] = input["address_code"]
	update["address_info"] = input["address_info"]
	update["is_default"] = input["is_default"]
	update["time_u"] = thisTime
	userAddressSql, args := sql_aid.PgTable(model.TableByUserAddress).Where("id = ?", input["id"]).Where("user_id = ?", userId).UpdateByMap(&update)

	_, err = tx.Exec(userAddressSql, args...)
	if err != nil {
		gogo_log.Error("server_UserAddressByEdit", "收货地址编辑", err.Error())
		return errors.New("添加失败")
	}
	tx.Commit()
	return nil
}

func UserAddressByEditIsDefault(userId int64, id int64) error {
	tx, err := model.PgSql.Begin()
	defer tx.Rollback()

	update := make(map[string]interface{})
	update["is_default"] = model.UserAddressByDefaultNo
	updateSql, args := sql_aid.PgTable(model.TableByUserAddress).Where("user_id = ?", userId).UpdateByMap(&update)
	_, err = tx.Exec(updateSql, args...)
	if err != nil {
		gogo_log.Error("server_UserAddressByEdit", "重置用户默认选项失败", err.Error())
		return errors.New("添加失败")
	}

	//时间
	thisTime := time.Now().Format(time.RFC3339)
	updateDefault := make(map[string]interface{})
	updateDefault["is_default"] = model.UserAddressByDefaultYes
	updateDefault["time_u"] = thisTime
	userAddressSql, args := sql_aid.PgTable(model.TableByUserAddress).Where("id = ?", id).Where("user_id = ?", userId).UpdateByMap(&updateDefault)

	_, err = tx.Exec(userAddressSql, args...)
	if err != nil {
		gogo_log.Error("server_UserAddressByEdit", "收货地址编辑", err.Error())
		return errors.New("添加失败")
	}
	tx.Commit()
	return nil
}

// UserAddressByDel 删除收货地址
func UserAddressByDel(userId int64, userAddressIds string) error {
	userAddressIdsBySplitString := strings.Split(userAddressIds, ",")
	userAddressIdsBySplitInterface := make([]interface{}, 0)
	for _, val := range userAddressIdsBySplitString {
		userAddressIdsBySplitInterface = append(userAddressIdsBySplitInterface, val)
	}
	delSql, args := sql_aid.PgTable(model.TableByUserAddress).WhereIn("id", userAddressIdsBySplitInterface...).Where("user_id = ?", userId).Delete()
	_, err := model.PgSql.Exec(delSql, args...)
	if err != nil {
		gogo_log.Error("server_UserAddressByDel", "删除收货地址", err.Error())
		return errors.New("操作失败")
	}
	return nil
}
